<?php

class FicheVitrineListModuleFrontController extends VitrineFrontController {
	
	public function initContent() {
		
		parent :: initContent();
		$abo = $this->checkAbo();		
		
		$random = rand(0, 1000);
		$weightModulator = Fiche::WEIGHT_MODULATOR;
		$customer = Context::getContext()->customer;
		
		$link = new Link();
		
		$linkModule = $link->getModuleLink('fichevitrine', 'default');
		$linkListModel = $link->getModuleLink('fichevitrine', 'list', array("type"=>Fiche::FICHE_MODELE));
		$linkListFiche = $link->getModuleLink('fichevitrine', 'list', array("type" => Fiche::FICHE));
		$linkList = $link->getModuleLink('fichevitrine', 'list');
		$linkAddFiche = $link->getModuleLink('fichevitrine', 'addfiche');
		$linkAddModel = $link->getModuleLink('fichevitrine', 'addmodel');
		$linkPrint = $link->getModuleLink('fichevitrine', 'print');
		$linkSaveFiche = $link->getModuleLink('fichevitrine', 'savefiche');
		$linkSaveImage = $link->getModuleLink('fichevitrine', 'saveimage');
		$linkEditorModel = $link->getModuleLink('fichevitrine', 'editormodel');
		$linkEditorFiche = $link->getModuleLink('fichevitrine', 'editorfiche');
		
		// LVM : on remonte info dans la classe Fiche
		/*
		$types = array();
		$types[Fiche::ALL] = 'Tous';
		$types[Fiche::FICHE] = 'Fiche';
		$types[Fiche::FICHE_MODELE] = 'Modèle';
		*/
		$types = Fiche::getTypes(true);
		$orientations = Fiche::getOrientations(true);
		$transacts = Fiche::getTransacts(true);
		
		$formats = Fiche::getFormats();
		//
		
		// LVM
		//$currentType = Fiche::ALL;
		$currentType = -1;
		
		$currentFicheName = '';
		$currentMaxWeight = '';
		$currentMinWeight = '';
		$currentMinDate = '';
		$currentMaxDate = '';
		$currentSearchText = '';
		$currentOrientation = -1;
		$currentFormat = -1;
		$currentTheme = -1;
		$currentGoodType = -1;
		
		// LVM
		//$currentRent = -1;
		$currentTransact = -1;
		
		// LVM
		$currentTransact = -1;
		
		$conditions = array();
		
		$themes = FicheTheme::findAll();
		$goodTypes = FicheProp::findAll();
		
		//delete
		if ( isset($_POST['deleteList']) && !empty($_POST['deleteList']) )
		{
			$tab = explode(',', $_POST['deleteList']);
			$i = count($tab);
			
			while ( --$i > -1 )
			{
				Fiche::killFicheById( (int)($tab[$i]) );
			}
			
			Tools::redirect($linkList);
		}
		
		// texte
		if ( isset($_POST['searchText']) && !empty($_POST['searchText']) )
		{
			$currentSearchText = $_POST['searchText'];
			
			$conditions['data'] = array('column'=>'data', 'value'=>$currentSearchText, 'operator'=>'LIKE');
		}
		
		// filtre sur le nom
		if ( isset($_POST['ficheName']) && !empty($_POST['ficheName']) )
		{
			$currentFicheName = $_POST['ficheName'];
			
			$conditions['name'] = array('column'=>'name', 'value'=>$currentFicheName, 'operator'=>'LIKE');
		}		
		
		// filtre sur l'orientation
		if ( isset($_POST['orientation']) )
		{
			$currentOrientation = (int)($_POST['orientation']);
			
			if( $currentOrientation != -1 )
				$conditions['orientation'] = $currentOrientation;
		}		
		
		// filtre sur le format
		if ( isset($_POST['format']) )
		{
			$currentFormat = $_POST['format'];
			
			if( $currentFormat != -1 )
				$conditions['format'] = $currentFormat;
		}
		
		// filtre sur le type
		
		// LVM : type peut être dans l'url...
		//if ( isset($_REQUEST['type']) || isset($_REQUEST['type']) )
		if ( isset($_REQUEST['type']) )
		{
			$currentType = (int)($_REQUEST['type']);
			
			// LVM
			//if ( $currentType != Fiche::ALL )
			if ( $currentType != -1 )
			{
				$conditions['type'] = $currentType;
			}
		}
		
		//filtre sur le poids max
		if ( isset($_POST['maxWeight']) && !empty($_POST['maxWeight']) )
		{
			$currentMaxWeight = (float)($_POST['maxWeight']) * $weightModulator;
			$conditions['weight'] = array('column'=>'weight', 'value'=>$currentMaxWeight, 'operator'=>'<=');
		}
		
		//filtre sur le poids min
		if ( isset($_POST['minWeight']) && !empty($_POST['minWeight']) )
		{
			$currentMinWeight = (float)($_POST['minWeight']) * $weightModulator;
			$conditions['weight_min'] = array('column'=>'weight', 'value'=>$currentMinWeight, 'operator'=>'>=');
		}
		
		//filtre sur la date min
		if ( isset($_POST['minDate'] ) && !empty($_POST['minDate']) )
		{
			$currentMinDate = $_POST['minDate'];
			$conditions['date_min'] = array('column'=>'date', 'value'=>$currentMinDate, 'operator'=>'>=');
		}						
		
		//filtre sur la date max
		if ( isset($_POST['maxDate'] ) && !empty($_POST['maxDate']) )
		{
			$currentMaxDate = $_POST['maxDate'];
			$conditions['date_max'] = array('column' => 'date', 'value' => $currentMaxDate.' 23:59:59', 'operator' => '<=');
		}				
		
		//filtre sur les themes
		if ( isset($_POST['theme'] ) )
		{
			$currentTheme = (int)($_POST['theme']);
			
			if( $currentTheme != -1 )
				$conditions['theme'] = $currentTheme;
		}
		//filtre sur les types de biens
		if ( isset($_POST['goodType'] ) )
		{
			$currentGoodType = (int)($_POST['goodType']);
			
			if( $currentGoodType != -1 )
				$conditions['propId'] = $currentGoodType;
		}
		
		//filtre sur les types location/vente
		// LVM
		/*
		if ( isset($_POST['rent'] ) )
		{
			$currentRent = (int)($_POST['rent']);
			
			if( $currentRent != -1 )
				$conditions['rent'] = $currentRent;
		}
		*/
		if ( isset($_POST['transact'] ) )
		{
			$currentTransact = (int)($_POST['transact']);
			
			if( $currentTransact != -1 )
				$conditions['rent'] = $currentTransact;
		}
			
		$conditions['id_customer'] = $customer->id; //filtre sur l'id customer
		
		// on cherche les données		
		$resultsPerPage = 10;
		$numModels = Fiche::getTotalFicheWhere(array('type' => Fiche::FICHE_MODELE, 'id_customer'=>$customer->id));
		$numFiches = Fiche::getTotalFicheWhere(array('type' => Fiche::FICHE, 'id_customer'=>$customer->id));
		$numPages = ceil( Fiche::getTotalFicheWhere($conditions) / $resultsPerPage ) - 1;
		$currentPage = (isset($_GET['page'])) ? (int)($_GET['page']) : 0;
		$currentPage = ( $currentPage > $numPages ) ? $numPages : $currentPage;
		$currentPage = ( $currentPage < 0 ) ? 0 : $currentPage;
		
		$start = $currentPage * $resultsPerPage;
		$end = $resultsPerPage;
		$fiches = Fiche::findFicheBy($conditions, $start, $end);
		$totalWeight = round(Fiche::getTotalWeight($customer) / $weightModulator , 2);
		
		foreach ( $fiches as $currentFiche )
		{
			$currentFiche-> weight = round($currentFiche-> weight / $weightModulator, 2);
			$currentFiche-> date_modif = date( Context::getContext()->language->date_format_lite, strtotime($currentFiche-> date_modif) );
			$currentFiche->date = date( Context::getContext()->language->date_format_lite, strtotime($currentFiche->date) );
		}
	
		$maxWeight = round($abo->storeSize / $weightModulator, 2);
		$percentWeight = round((float)( ($totalWeight / $maxWeight) * 100 ),2);
		
		$this->context->smarty->assign(array(
			"myTpldir"			=> _PS_MODULE_DIR_.'fichevitrine/views/templates/front/',
			'twoInOne' 			=> $abo->twoInOne,
			'numModels' 		=> $numModels,
			'numFiches' 		=> $numFiches,
			'numPages' 			=> $numPages,
			'currentPage' 		=> $currentPage,
			'fiches' 			=> $fiches,
			'linkModule' 		=> $linkModule,
			'linkEditorModel' 	=> $linkEditorModel,
			'linkEditorFiche' 	=> $linkEditorFiche,
			'linkList' 			=> $linkList,
			'linkListFiche' 	=> $linkListFiche,
			'linkListModel' 	=> $linkListModel,
			'linkPrint' 		=> $linkPrint,
			'linkAddFiche' 		=> $linkAddFiche,
			'linkHome' 			=> $linkModule,
			'linkAddModel' 		=> $linkAddModel,
			'moduleName' 		=> 'fichevitrine',
			'customer' 			=> $customer,
			'path' 				=> 'Fiche Vitrine',
			'currentType' 		=> $currentType,
			'currentFicheName' 	=> $currentFicheName,
			'currentMinWeight' 	=> $currentMinWeight / $weightModulator,
			'currentMaxWeight' 	=> $currentMaxWeight / $weightModulator,
			'currentMinDate' 	=> $currentMinDate,
			'currentMaxDate' 	=> $currentMaxDate,
			'currentTheme' 		=> $currentTheme,
			'currentSearchText' => $currentSearchText,
			
			// LVM
			//'currentRent' 		=> $currentRent,
			'currentTransact' 		=> $currentTransact,
			
			'currentGoodType' 	=> $currentGoodType,
			'currentOrientation'=> $currentOrientation,
			'currentFormat'		=> $currentFormat,
			
			'themes' 			=> $themes,
			'goodTypes' 		=> $goodTypes,
			'types'				=> $types,
			
			'nocache'			=> $random,
			'totalWeight'		=> $totalWeight,
			'maxWeight'			=> $maxWeight,
			'percentWeight'		=> $percentWeight,
			'abo'				=> $abo,
			
			// LVM : on remonte info dans la classe Fiche
			//'orientations' => array("paysage"=>Fiche::LANDSCAPE, "portrait"=>Fiche::PORTRAIT),
			'orientations' => $orientations,
			
			// LVM : addition info type de transactions Location ou Vente (ou autres plus tard : Viager par ex)
			'transacts' => $transacts,
			
			'manual' 			=> Fiche::getManURI(),
						
			// LVM
			//"formats" => array_keys(Fiche::getFormats())
			"formats" => array_keys($formats)
		));
		
		$this->setTemplate('list.tpl');
		//poids, titre, user_id
	}
	
}
